package gui;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

import commands.LoginCheckCommand;
import containers.NewLoanContainer;
import containers.StudentContainer;
import dbadapters.ChangeStudentdb;
import dbadapters.checkConnection;

import entities.Student;

/**
 * 
 * @author eks691
 * 
 */
public class EmployeeNewStudentLoanInfoPanel extends JPanel {

	private static final long serialVersionUID = 1L;

	/**
	 * Student the panel is displaying
	 */
	Student student;

	/** id of the student in the system */
	private int id;

	public EmployeeNewStudentLoanInfoPanel(Student s, int i) {
		id = i;
		student = s;
		// System.out.println("New class; Name of Student: " + s.getName());

		// panels are set out horizontally
		setBorder(new TitledBorder(new EtchedBorder(), "Student Information"));
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		EmployeeStudentInfoPanel sInfoPanel = new EmployeeStudentInfoPanel(
				student);
		add(sInfoPanel);
		EmployeeKinInfoPanel kInfoPanel = new EmployeeKinInfoPanel(student);
		add(kInfoPanel);
		EmployeeFinInfoPanel fInfoPanel = new EmployeeFinInfoPanel(student);
		add(fInfoPanel);
		EmployeeLoanInfoPanel lInfoPanel = new EmployeeLoanInfoPanel(student);
		add(lInfoPanel);
		ButtonPanel bPanel = new ButtonPanel();
		add(bPanel);
	}

	/**
	 * Construct a panel for the buttons for the panel
	 */
	private class ButtonPanel extends JPanel {

		private static final long serialVersionUID = 1L;

		private ButtonPanel() {

			setLayout(new GridLayout(2, 3, 70, 20));

			JButton verifyEnrollment = new JButton("Verify Enrollment");
			add(verifyEnrollment);
			JButton riskAssessment = new JButton("Risk Assessment");
			add(riskAssessment);
			// JButton calculateLoan = new JButton("Calculate Loan");
			// add(calculateLoan);
			JButton acceptLoan = new JButton("Approve Loan");
			add(acceptLoan);
			JButton declineLoan = new JButton("Decline Loan");
			add(declineLoan);
			JButton back = new JButton("Back");
			add(back);

			// add listeners for the buttons
			back.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent event) {
					getTopLevelAncestor().setVisible(false);
					MainMenuFrame frame = new MainMenuFrame(
							LoginCheckCommand.UserType.EMPLOYEE, id);
					frame.setVisible(true);
					EmployeeNewLoanPanel newPanel = new EmployeeNewLoanPanel(id);
					frame.changePanel(newPanel);
				}
			});

			verifyEnrollment.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent event) {
					JFrame frame = new JFrame();
					// frame.setTitle("Verify Enrollment");
					JOptionPane.showMessageDialog(frame,
							"Phone " + student.getInstitution());
				}
			});

			riskAssessment.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent event) {
					/*
					 * JFrame frame = new JFrame();
					 * JOptionPane.showMessageDialog(frame,
					 * "Assessing Risk...");
					 */
					getTopLevelAncestor().setVisible(false);
					MainMenuFrame frame2 = new MainMenuFrame(
							LoginCheckCommand.UserType.EMPLOYEE, id);
					frame2.setVisible(true);
					EmployeeRiskAssessmentPanel newPanel = new EmployeeRiskAssessmentPanel(
							student.getID(), id);
					frame2.changePanel(newPanel);
				}
			});

			acceptLoan.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent event) {
					// accepting the loan for the student
					JFrame frame = new JFrame();
					JOptionPane.showMessageDialog(frame,
							"Loan accepted for amount: "
									+ student.getLoan().getAmountRequested());
					student.getLoan().setApproval(true);
					student.getLoan().setIsNew(false);
					student.getLoan().setAmountGiven(
							student.getLoan().getAmountRequested());
					StudentContainer.dictionary().remove(student.getID());
					StudentContainer.dictionary().put(student.getID(), student);
					// remove student from new loan container
					NewLoanContainer.dictionary().remove(student.getID());

					// update student info in db if possible
					checkConnection c = new checkConnection();

					if (c.isConnectionValid()) {
						new ChangeStudentdb(student);
					} else {
						System.out.println(c.getErrorMessage());
					}

					getTopLevelAncestor().setVisible(false);
					MainMenuFrame myFrame = new MainMenuFrame(
							LoginCheckCommand.UserType.EMPLOYEE, id);
					myFrame.setVisible(true);
					EmployeeNewLoanPanel newPanel = new EmployeeNewLoanPanel(id);
					myFrame.changePanel(newPanel);

				}
			});

			declineLoan.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent event) {
					// accepting the loan for the student
					JFrame frame = new JFrame();
					JOptionPane.showMessageDialog(frame,
							"Loan declined for amount: "
									+ student.getLoan().getAmountRequested());
					student.getLoan().setApproval(false);
					student.getLoan().setIsNew(false);
					StudentContainer.dictionary().remove(student.getID());
					StudentContainer.dictionary().put(student.getID(), student);
					// remove student from new loan container
					NewLoanContainer.dictionary().remove(student.getID());

					// update student info in db if possible
					checkConnection c = new checkConnection();

					if (c.isConnectionValid()) {
						new ChangeStudentdb(student);
					} else {
						System.out.println(c.getErrorMessage());
					}

					getTopLevelAncestor().setVisible(false);
					MainMenuFrame myFrame = new MainMenuFrame(
							LoginCheckCommand.UserType.EMPLOYEE, id);
					myFrame.setVisible(true);
					EmployeeNewLoanPanel newPanel = new EmployeeNewLoanPanel(id);
					myFrame.changePanel(newPanel);
				}
			});
		}
	}
}
